package com.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
			
		
			//判断是不是登录
			HttpServletRequest req=(HttpServletRequest)request;
			HttpServletResponse resp=(HttpServletResponse)response;
			Object obj=req.getSession().getAttribute("user");
			
			String uri=req.getRequestURI();
			String context=req.getContextPath();
			
			
			
			if(obj==null){
				if(uri.equals(context+"/success.html")
						|| uri.equals(context+"/loginSuc")){
					resp.sendRedirect("login.html");
					return;
				}
			}
			else{
				if(uri.equals(context+"/register.html") 
						|| uri.equals(context+"/login.html")
						|| uri.equals(context+"/login")
						|| uri.equals(context+"/register")){
					resp.sendRedirect("success.html");
					return;
				}
			}
			
			
			chain.doFilter(req, resp);
			
	}

	@Override
	public void destroy() {

	}

}
